<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增素材中心')" />
    <th:block th:include="include :: datetimepicker-css" />
</head>

<style>
    .divImg {
        float: left;
        margin: 2px;
        padding: 2px;
        height: 108px;
        width: 100px;
        border: 1px solid #dcdcdc;
        cursor: pointer;
    }
</style>

<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-material-add">
<!--            <div class="form-group">-->
<!--                <label class="col-sm-3 control-label">头像访问路径：</label>-->
<!--                <div class="col-sm-8">-->
<!--                    <textarea name="mainUrl" class="form-control"></textarea>-->
<!--                </div>-->
<!--            </div>-->
<!--            <div class="form-group">-->
<!--                <label class="col-sm-3 control-label">头像真实路径：</label>-->
<!--                <div class="col-sm-8">-->
<!--                    <textarea name="mainUrlReal" class="form-control"></textarea>-->
<!--                </div>-->
<!--            </div>-->

            <div class="form-group">
                <label class="col-sm-3 control-label">头像(233*233：</label>
                <div class="col-sm-8">
                    <button type="button" class="layui-btn" id="upload_main">上传图片</button>
                    <div class="layui-upload-list">
                        <input type="hidden" id="mainUrl" name="mainUrl">
                        <input type="hidden" id="mainUrlReal" name="mainUrlReal">
                        <p id="show_main"></p>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">姓名：</label>
                <div class="col-sm-8">
                    <input name="name" class="form-control" type="text">
                </div>
            </div>
<!--            <div class="form-group">    -->
<!--                <label class="col-sm-3 control-label">创建时间：</label>-->
<!--                <div class="col-sm-8">-->
<!--                    <div class="input-group date">-->
<!--                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>-->
<!--                        <input name="created" class="form-control" placeholder="yyyy-MM-dd" type="text">-->
<!--                    </div>-->
<!--                </div>-->
<!--            </div>-->
            <div class="form-group">
                <label class="col-sm-3 control-label">素材内容：</label>
                <div class="col-sm-8">
                    <textarea name="materialContent" class="form-control"></textarea>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label">素材图(233*233)</label>
                <div class="col-sm-8">
                    <div style="text-align: left;">
                        <button type="button" class="layui-btn" id="upload_img" >上传图片</button>
                    </div>
                    <div class="layui-upload-list">
                        <div id="img-url" style="padding: 5px;"></div>
                        <!--<input type="hidden" id="logo" name="logo">-->
                        <!--<p id="show_img"></p>-->
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">是否展示：</label>
                <div class="col-sm-8">
                    <div class="radio-box" th:each="dict : ${@dict.getType('material_show')}">
                        <input type="radio" th:id="${'status_' + dict.dictCode}" name="isShow" th:value="${dict.dictValue}"
                               th:checked="${dict.default}">
                        <label th:for="${'status_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">排序：</label>
                <div class="col-sm-8">
                    <input name="sort" class="form-control" type="text" required>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer"/>
    <th:block th:include="include :: datetimepicker-js"/>
    <th:block th:include="include :: sortable-js"/>
    <th:block th:include="include :: select2-js" />
    <script type="text/javascript">
        var prefix = ctx + "system/material"
        $("#form-material-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-material-add').serialize());
            }
        }

        $("input[name='created']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        layui.use('upload', function () {
            var $ = layui.jquery
                , upload = layui.upload;

            //普通图片上传
            upload.render({
                elem: '#upload_main'
                , url: '/common/upload' //改成您自己的上传接口
                , done: function (res) {
                    //如果上传失败
                    if (res.code > 0) {
                        return layer.msg('上传失败');
                    }
                    //上传成功
                    $("#show_main").html("<img src='" + res.url + "' style='width:80px;height:80px;'>");
                    $("#mainUrl").val(res.url);
                    $("#mainUrlReal").val(res.realPath);
                }
            });


            var imgId = 0;
            var container = document.getElementById("img-url");
            var sort = Sortable.create(container);
            // var sortDetail = Sortable.create(containerDetail);

            //普通图片上传
            upload.render({
                elem: '#upload_img'
                , url: '/common/upload?type=0' //改成您自己的上传接口
                , multiple: true
                ,before: function(obj){
                    //预读本地文件示例，不支持ie8
                    obj.preview(function(index, file, result){
                        // $('#img-url').append('<img src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img">')

                        var name= file.name.split(".")[0];
                        console.log(name)
                        $("#img-url").append("<div id='div" + name + "'><div  class='divImg'>" +
                            "<img  id='src" + name + "' src='' style='width:100px;height: 80px;margin-bottom: 1px;'><br>" +

                            "<i style='float: right;padding-top: 4px;' class='fa fa-close' onclick=\"delImg('div" + name + "')\"></i></div></div>");
                        // $("#logo").val(res.url);
                    });
                }
                , done: function (res) {
                    //如果上传失败
                    if (res.code > 0) {
                        return layer.msg('上传失败');
                    }
                    imgId++;
                    $("#div"+res.name).replaceWith("<div id='imgId" + imgId + "' class='divImg'>" +
                        "<img  onclick=\"setImg('imgId" + imgId + "')\" src='" + res.url + "' style='width:100px;height: 80px;margin-bottom: 1px;'><br>" +
                        "<input type='hidden' name='imgBannerUrl' value='" + res.url + "'>" +
                        "<input type='hidden' name='imgBannerRealPath' value='" + res.realPath + "'>" +
                        "<i style='float: right;padding-top: 4px;' class='fa fa-close' onclick=\"delImg('imgId" + imgId + "')\"></i></div>");

                    sort.destroy();
                    sort = Sortable.create(container);


                }
            });
        });

        function setImg(id) {
            $("#img").find("div").each(function () {
                $(this).removeClass("divImgD").addClass("divImg");
            });
            $("#" + id).removeClass("divImg");
            $("#" + id).addClass("divImgD");
        }

        function delImg(id) {
            $("#" + id).remove();
        }

        function getImg() {
            var imgs = [];

            $("#img-url").find("div").each(function () {
                var img = {};
                $(this).find("img").each(function () {
                    img.url = $(this).attr("src");
                });
                imgs.push(img);
            });
            return imgs;
        }

    </script>
</body>
</html>